﻿
Imports System.Xml.Serialization
Imports System.Text
Imports iConverter.Extensions
Imports System.Runtime.Serialization

Namespace Configuration

    ''' <summary>
    ''' 用于储存 使用的后期处理插件
    ''' </summary>
    ''' <remarks></remarks>
    <Serializable()>
    Public Class PluginPostProcessingList
        Inherits List(Of PluginPostProcessingItem)
        Implements IXmlSerializable, ICloneable

        Public Sub New()

        End Sub
        Public Sub New(ByVal capacity As Integer)
            MyBase.New(capacity)
            
        End Sub
        Public Sub New(ByVal collection As IEnumerable(Of PluginPostProcessingItem))
            MyBase.New(collection)
            
        End Sub



        Public Overloads Sub Add(providerId As Guid, postProcessingId As Guid, processerId As Guid)
            Me.Add(New PluginPostProcessingItem With {.ProviderID = providerId ,
                                                       .PostProcessingID = postProcessingId,
                                                       .SelectProcesserID = processerId})

        End Sub


        Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
            Return Nothing
        End Function

        Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml
            Dim xml = XElement.Load(reader)

            Dim items = From item In xml...<item>
                        Select New With {.Provider = item.@Provider,
                                         .PostProcessing = item.@PostProcessing,
                                         .SelectProcesserID = item.@SelectProcesserID}



            Dim pid, ppid, spid As Guid



            For Each i In items
                If Guid.TryParse(i.Provider, pid) AndAlso Guid.TryParse(i.PostProcessing, ppid) AndAlso Guid.TryParse(i.SelectProcesserID, spid) Then
                    Me.Add(pid ,ppid,spid )

                End If
            Next


        End Sub

        Public Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
            Dim xml = <PluginPostProcessings>
                          <%= From item In Me
                              Select <item Provider=<%= item.ProviderID %>
                                         PostProcessing=<%= item.PostProcessingID %>
                                         SelectProcesserID=<%= item.SelectProcesserID %>/>
                          %>
                      </PluginPostProcessings>



            xml.WriteTo(writer)

        End Sub

#Region "Clone 实现"

        Function Clone() As PluginPostProcessingList
            Return New PluginPostProcessingList(Me)
        End Function

        Private Function ImeClone() As Object Implements System.ICloneable.Clone
            Return Me.Clone
        End Function

#End Region
    End Class


    ''' <summary>
    ''' 用于保存使用的后期插件数据设置的记录项
    ''' </summary>
    ''' <remarks></remarks>
    Public Class PluginPostProcessingItem

        ''' <summary>
        ''' 插件提供器ID
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ProviderID As Guid


        ''' <summary>
        ''' 后期处理插件 ID
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property PostProcessingID As Guid


        ''' <summary>
        ''' 使用的处理器ID
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property SelectProcesserID As Guid



    End Class

End Namespace

